package com.example.springbootbabyu.controller;

import com.example.springbootbabyu.entity.CartItemDTO;
import com.example.springbootbabyu.entity.CreateOrder;
import com.example.springbootbabyu.entity.OrderListItemDTO;
import com.example.springbootbabyu.service.MemberService;

import com.example.springbootbabyu.utils.JwtTokenUtil;
import com.example.springbootbabyu.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/19 22:44
 */
@RestController
@RequestMapping("/member")
@CrossOrigin
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;
    private final JwtTokenUtil jwtTokenUtil;
    private MemberController( JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }
    @PostMapping("/cart/merge")
    public ResultUtil mergeCart1(@RequestHeader("Authorization") String token, @RequestBody(required = false) @Validated List<CartItemDTO> cartItemDTO) {
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.mergeCart(userId, cartItemDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @GetMapping("/cart/merge")
    public ResultUtil getMergeCart(@RequestHeader(name = "Authorization") String token) {
//        token = token.replace("Bearer ", "");
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        String userId = (String) map.get("id");
//        if (map == null) {
//            ResultUtil resultUtil = new ResultUtil();
//            resultUtil.setCode(0);
//            resultUtil.setMsg("token失效");
//            return resultUtil;
//        }
//        ResultUtil resultUtil = new ResultUtil();
//        resultUtil.setCode(1);
//        resultUtil.setMsg("成功");
//        resultUtil.setResult(memberService.getMergeCart(userId)); // 完善代码
//        return resultUtil;
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            log.info("userId: {}", userId);
            return ResultUtil.success(1, "成功", memberService.getMergeCart(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @GetMapping("/cart")
    public ResultUtil getCard(@RequestHeader(name = "Authorization") String token) {
//        token = token.replace("Bearer ", "");
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        String userId = (String) map.get("id");
//        if (map == null) {
//            ResultUtil resultUtil = new ResultUtil();
//            resultUtil.setCode(0);
//            resultUtil.setMsg("token失效");
//            return resultUtil;
//        }
//        ResultUtil resultUtil = new ResultUtil();
//        resultUtil.setCode(1);
//        resultUtil.setMsg("成功");
//        resultUtil.setResult(memberService.getCard(userId));
//        return resultUtil;
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.getCard(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @PostMapping("/cart")
    public ResultUtil addCart(@RequestHeader(name = "Authorization") String token, @RequestBody @Validated CartItemDTO cartItemDTO) {
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.addCart(userId, cartItemDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @DeleteMapping("/cart")
    public ResultUtil deleteCart(@RequestHeader(name = "Authorization") String token, @RequestBody List<String> ids ) {
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.deleteCart(userId, ids));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @PostMapping("/order")
    public ResultUtil createOrder(@RequestHeader(name = "Authorization") String token, @RequestBody CreateOrder createOrder) {
        try {
            // 1. 验证用户并获取用户ID
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.createOrder(userId, createOrder));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @GetMapping("/order/{id}")
    public ResultUtil getOrderDetail(
            @PathVariable String id,
            @RequestHeader(name = "Authorization") String token) {
        try {
            log.info("id: {}", id);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.getOrderDetail(userId, id));
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @GetMapping("/order")
    public ResultUtil queryUserOrders(
            @RequestParam(required = false) Integer orderState,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader(name = "Authorization") String token) {

        try {
            log.info("orderState: {}, page: {}, pageSize: {}", orderState, page, pageSize);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.queryUserOrders(userId, orderState, page, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
    @GetMapping("/order/pre")
    public ResultUtil getPreOrder(@RequestHeader(name = "Authorization") String token) {
//        token = token.replace("Bearer ", "");
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        String userId = (String) map.get("id");
//        if (map == null) {
//            ResultUtil resultUtil = new ResultUtil();
//            resultUtil.setCode(0);
//            resultUtil.setMsg("token失效");
//            return resultUtil;
//        }
//        ResultUtil resultUtil = new ResultUtil();
//        resultUtil.setCode(1);
//        resultUtil.setMsg("成功");
//        resultUtil.setResult(memberService.getPreOrder(userId));
//        return resultUtil;
        try {
            log.info("token: {}", token);
            String userId = jwtTokenUtil.getUserIdFromToken(token);
            return ResultUtil.success(1, "成功", memberService.getPreOrder(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, "失败");
        }
    }
//    @PostMapping("/order")
//    public ResultUtil createOrder() {
//
//    }
}
